function detectmob() {
    return !!(navigator.userAgent.match(/Android/i)
        || navigator.userAgent.match(/webOS/i)
        || navigator.userAgent.match(/iPhone/i)
        || navigator.userAgent.match(/iPad/i)
        || navigator.userAgent.match(/iPod/i)
        || navigator.userAgent.match(/BlackBerry/i)
        || navigator.userAgent.match(/Windows Phone/i));
}

function startMove(obj, json, fn) {
    clearInterval(obj.timer);
    obj.timer = setInterval(function () {
        var speed = 0;
        var bad = 0;
        var flag = true;
        for (var arra in json) {
            if (arra === 'opacity') {
                bad = Math.round(parseFloat(getStyle(obj, arra)) * 100);
            }
            else {
                bad = parseInt(getStyle(obj, arra));
            }
            speed = (json[arra] - bad) / 5;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
            if (bad !== json[arra]) {
                flag = false;
            }
            if (arra === 'opacity') {
                obj.style.filter = 'alpha(opacity:' + (bad + speed) + ')';
                obj.style[arra] = (bad + speed) / 100;
            }
            else {
                obj.style[arra] = bad + speed + "px";
            }
        }
        if (flag) {
            clearInterval(obj.timer);
            if (fn) {
                fn();
            }
        }
    }, 30)
}

function getStyle(obj, attr) {
    if (obj.currentStyle) {
        return obj.currentStyle[attr];
    }
    else {
        return getComputedStyle(obj, false)[attr];
    }
}

function selectByClass(obj, className) {
    var allSelect = obj.getElementsByTagName('*');
    var resultList = [];
    for (var i = 0; i < allSelect.length; i++) {
        if (allSelect[i].className === className) {
            resultList[resultList.length] = allSelect[i];
        }
    }
    return resultList;
}

function byId(objName){
    return document.getElementById(objName);
}

function getNinePictures(image, imgUrl, clupList, becomeAdd, isCrosswise) {
    //alert(3)
    var originalPicture = new Image();
    originalPicture.setAttribute("crossOrigin", 'Anonymous');
    originalPicture.src = imgUrl;
    originalPicture.onload = function (ev) {
        var _this = this;
        this.imgCanvas = document.createElement('canvas');
        this.ctx = this.imgCanvas.getContext('2d');
        this.colExpend = function (clupList, becomeAdd) {
            _this.imgCanvas.width = _this.width;
            _this.imgCanvas.height = _this.height + becomeAdd[0];
            for (var i = 0; i < _this.imgCanvas.height; i++) {
                if (i > clupList[0] && i < (clupList[1] + becomeAdd[0])) {
                    _this.ctx.drawImage(_this, 0, clupList[0] + 1, _this.imgCanvas.width, 1, 0, i, _this.imgCanvas.width, 1);
                } else if (i >= clupList[1] + becomeAdd[0]) {
                    _this.ctx.drawImage(_this, 0, i - becomeAdd[0], _this.imgCanvas.width, 1, 0, i, _this.imgCanvas.width, 1);
                }
                else {
                    this.ctx.drawImage(_this, 0, i, _this.imgCanvas.width, 1, 0, i, _this.imgCanvas.width, 1);
                }
            }
        };//纵向延伸
        this.rawExtend = function (clupList, becomeAdd) {
            _this.imgCanvas.width = _this.width + becomeAdd[0];
            _this.imgCanvas.height = _this.height;
            for (var i = 0; i < _this.imgCanvas.width; i++) {
                if (i > clupList[0] && i < (clupList[1] + becomeAdd[0])) {
                    _this.ctx.drawImage(_this, clupList[0] + 1, 0, 1, _this.imgCanvas.height, i, 0, 1, _this.imgCanvas.height);
                } else if (i >= clupList[1] + becomeAdd[0]) {
                    _this.ctx.drawImage(_this, i - becomeAdd[0], 0, 1, _this.imgCanvas.height, i, 0, 1, _this.imgCanvas.height);
                }
                else {
                    _this.ctx.drawImage(_this, i, 0, 1, _this.imgCanvas.height, i, 0, 1, _this.imgCanvas.height);
                }

            }
        };//横向延伸
        if (clupList.length === 2) {
            if (isCrosswise === false) {
                _this.colExpend(clupList, becomeAdd);
            }
            else {
                _this.rawExtend(clupList, becomeAdd);
            }
            image.style.backgroundImage = 'url(' + _this.imgCanvas.toDataURL() + ')';
            image.style.backgroundSize = '100% 100%';
        }//横向或纵向
        else if (clupList.length === 4) {
            if (becomeAdd.length === 2) {
                _this.rawExtend(clupList, becomeAdd);
                getNinePictures(image, _this.imgCanvas.toDataURL(), [clupList[2], clupList[3]], [becomeAdd[1]], false);
            }
        }
        else if (clupList.length === 0) {
            //alert(2)
            if (becomeAdd.length === 1) {
                var clupList2 = [
                    _this.width / 2,
                    _this.width / 2 + 10
                ];
                this.rawExtend(clupList2, becomeAdd);
            }
            else if (becomeAdd.length === 2) {
                //alert(1)
                var clupList3 = [
                    _this.width / 2,
                    _this.width / 2,
                    _this.height / 2,
                    _this.height / 2
                ];
                _this.rawExtend(clupList3, becomeAdd);
                getNinePictures(image, _this.imgCanvas.toDataURL(), [clupList3[2], clupList3[3]], [becomeAdd[1]], false);
                //console.log(this.imgCanvas.width)
            }
            else if (becomeAdd.length === 0) {
                //alert(0)
                if (isCrosswise === false) {
                    var clupList4 = [
                        _this.height / 2,
                        _this.height / 2
                    ];
                    var becomeAdd2 = [
                        _this.height
                    ];
                    this.colExpend(clupList4, becomeAdd2);
                }
                else {
                    var clupList5 = [
                        _this.width / 2,
                        _this.width / 2 + 1
                    ];
                    var becomeAdd3 = [
                        _this.width
                    ];
                    //console.log(clupList5);
                    //console.log(becomeAdd3);
                    _this.rawExtend(clupList5, becomeAdd3);
                }

            }
        }
    }
}
